#!/bin/sh

revision="persistance v0.1.3 (2023-06-14 by MIBonk)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS 1> /dev/null
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

if [ ! -z $2 ]; then
VALUE=$1
else
VALUE=""
fi

	case $VALUE in

	-r) {

trap '' 2

PART=$2
START=$3
END=$4

if [ -z $END ]; then
	END=$(echo $START + 1 | $BC)
else
	if [[ $END -le $START ]]; then
		END=$(echo $START+1 | $BC)
	else
		END=$(echo $END+1 | $BC)
	fi
fi

echo -ne "part-dec,part-hex,a$DDr-dec,a$DDr-hex,data,object\n"

if [ -f /net/mmx/mnt/app/eso/bin/dumb_persistence_reader ]; then
	while [ $START -lt $END ]; do
		VALUEHEX=0x$(echo "obase=16; $START" | $BC)
		PARTHEX=0x$(echo "obase=16; $PART" | $BC)
		echo -ne "$PART,$PARTHEX,$START,$VALUEHEX,"
		OBJECT=",blob"
		SUBJECT="$($PERSR $PART $START -t blob 2> /dev/null)"
		if [ -z "$SUBJECT" ]; then
			OBJECT=",int"
			SUBJECT="$($PERSR $PART $START -t int 2> /dev/null)"
			if  [ -z "$SUBJECT" ]; then
				OBJECT=",string"
				SUBJECT="$($PERSR $PART $START -t string 2> /dev/null)"
				if  [ -z "$SUBJECT" ]; then
					SUBJECT=""
					OBJECT=",error"
				fi
			fi
		fi
		echo -ne $SUBJECT
		echo -ne $OBJECT
		echo -ne "\n"
	START=$(echo $START +1 | $BC)
	done
else
	while [ $START -lt $END ]; do
		VALUEHEX=0x$(echo "obase=16; $START" | $BC)
		PARTHEX=0x$(echo "obase=16; $PART" | $BC)
		echo -ne "$PART,$PARTHEX,$START,$VALUEHEX,"
		OBJECT=",blob"
		SUBJECT="$($UDP -key $START -ns $PART -type b | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null)"
		if [ -z "$SUBJECT" ]; then
			OBJECT=",int"
			SUBJECT="$($UDP -key $START -ns $PART -type i | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null)"
			if  [ -z "$SUBJECT" ]; then
				OBJECT=",string"
				SUBJECT="$($UDP -key $START -ns $PART -type s | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null)"
				if  [ -z "$SUBJECT" ]; then
					SUBJECT=""
					OBJECT=",error"
				fi
			fi
		fi
		echo -ne $SUBJECT
		echo -ne $OBJECT
		echo -ne "\n"
	START=$(echo $START +1 | $BC)
	done
fi
echo -ne "\n"

trap 2

return 2> /dev/null

	};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] <partition/namespace> <startkey> <endkey>"
		echo ""
		echo "Options:"
		echo "        -r 			read data"
		echo "        --help			show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
		
	};;

	esac

exit 0

